﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model.Contact;

using System.Data;
using Jzy.DAL.Contact;

using System.Web.UI;

using Frame.Lib.Core.Base;
using Frame.Lib.Core.Tree;
using Frame.Lib.Utilities;

namespace Jzy.BLL.Contact
{
    public class ContactBO : CommonOperate<ContactInfo>
    {
        private ContactDAL _contact;
        protected override BaseDAL DAL
        {
            get
            {
                return _contact;
            }
        }
        public ContactBO()
            : base("Contact")
        {
            _contact = new ContactDAL();
        }

        public System.Data.DataTable GetList(int dirID, int userID)
        {
            List<int> subIDs = (new TreeBO<TreeInfo>("ContactDir")).GetSubIDs(dirID);
            DataTable dt = _contact.GetList(subIDs, userID);
            return dt;
        }

        public void ExportExcel(object dataSrc)
        {
            DataView dv;
            if (dataSrc is DataTable)
            {
                dv = ((DataTable)dataSrc).DefaultView;
            }
            else
            {
                dv = (DataView)dataSrc;
            }

            StringBuilder sb = new StringBuilder(500);
            sb.Append("<table cellspacing='0' cellpadding='0' rules='all' border='1' >");
            sb.Append("<tr>");
            sb.Append("     <th>序号</th>");
            sb.Append("     <th>姓名</th>");
            sb.Append("     <th>手机</th>");
            sb.Append("     <th>MSN</th>");
            sb.Append("     <th>电子邮件</th>");
            sb.Append("</tr>");

            int index = 1;
            foreach (DataRowView row in dv)
            {
                sb.Append("<tr>");
                sb.Append("     <td>" + index + "</td>");
                sb.Append("     <td>" + row["Name"] + "</td>");
                sb.Append("     <td>" + row["Mobile"] + "</td>");
                sb.Append("     <td>" + row["WorkPhone"] + "</td>");
                sb.Append("     <td>" + row["MSN"] + "</td>");
                sb.Append("     <td>" + row["Email"] + "</td>");
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            WebUtil.ExportExcel(sb.ToString(), "外部联系人");
        }
    }
}
